package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import cn.qhuan.modules.dtos.ExhiTimeDto;
import cn.qhuan.modules.dtos.SpecialDto;
import cn.qhuan.modules.dtos.SpecialDto;
import cn.qhuan.modules.dtos.SpecialShowDto;
import cn.qhuan.modules.dtos.SpecialTimeDto;
import cn.qhuan.modules.entity.Special;
import cn.qhuan.modules.entity.Special;
import cn.qhuan.modules.entity.Works;
import cn.qhuan.modules.mapper.SpecialMapper;
import cn.qhuan.modules.mapper.SpecialMapper;
import cn.qhuan.modules.service.ISpecialService;

import cn.qhuan.modules.service.ISpecialService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 专题表
 * @Author: 司翔宇
 * @Date:   2020-12-22
 * @Version: V1.0
 */
@Service
public class SpecialServiceImpl extends ServiceImpl<SpecialMapper, Special> implements ISpecialService {

    @Autowired
    private SpecialMapper specialMapper;
    @Override
    public Result<?> findAllByKeyWord(SpecialDto specialDto) {
        //1.检查参数
        if(specialDto == null){
            return Result.error("缺少参数");
        }
        //分页参数检查
        specialDto.checkParam();
        //分页条件查询
        IPage pageParam =new Page(specialDto.getPage(),specialDto.getSize());
        LambdaQueryWrapper<Special> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        //按照关键字搜索
        if (StringUtils.isNotBlank(specialDto.getKeyWord())){
            lambdaQueryWrapper.like(Special::getName,specialDto.getKeyWord());
        }
        //封装结果进行返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(specialDto.getPage(),specialDto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> loadNewSpecList(SpecialTimeDto specialTimeDto) {
        //参数检查
        if (specialTimeDto == null ){
            return Result.error("缺少参数");
        }
        //分页检查
        specialTimeDto.checkParam();
        IPage pageParam =new Page(specialTimeDto.getPage(),specialTimeDto.getSize());
        LambdaQueryWrapper<Special> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Special> lambdaQueryWrapper2 =new LambdaQueryWrapper<>();

        if (ExhiTimeDto.getNowTime() != null ){
            lambdaQueryWrapper.gt(Special::getCreateTime,ExhiTimeDto.getNowTime()).orderByAsc(Special::getCreateTime);

        }
        //封装结果进行返回
        IPage pageList  =page(pageParam,lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(specialTimeDto.getPage(),specialTimeDto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        //进行判断，如果展示的最新数据达不到10条则直接按照时间排序找到最新的10条数据进行展示
        if (pageList.getRecords().size() <StatusConstos.SIZE){
            lambdaQueryWrapper2.orderByDesc(Special::getCreateTime);
            IPage pageList1 = page(pageParam,lambdaQueryWrapper2);
            PageResponseResult responseResult1 =new PageResponseResult(specialTimeDto.getPage(),specialTimeDto.getSize(), (int) pageList1.getTotal());
            responseResult1.setResult(pageList1.getRecords());
            return responseResult1;
        }else {
            return responseResult;
        }

    }

    @Override
    public Result<?> updateShowStatus(SpecialShowDto showDto, Integer type) {
        //1.检查参数
        if (showDto == null || showDto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询作品信息

        Special special = specialMapper.selectById(showDto.getId());
        if (special == null ){
            return Result.error("专题不存在");
        }
        //修改专题展示状态
        //不显示
        if (type.equals(StatusConstos.HIDDEN_STATUS)){
            updateShowSpec(special,StatusConstos.HIDDEN_STATUS);
        }else if (type.equals(StatusConstos.SHOW_STATUS)){
            updateShowSpec(special,StatusConstos.SHOW_STATUS);
        }
        return Result.OK("修改成功");
    }

    @Override
    public Result<?> findWorksListBySpecialId(SpecialDto dto) {
        //检查参数
        if (dto.getSpecialId() == null){
            return Result.error("缺少参数");
        }
        //设置分页条件
        dto.setPage((dto.getPage()-1)*dto.getSize());

        List<Works> worksList = specialMapper.findWorksListBySpecialId(dto);
        //统计数据
        int count =specialMapper.findWorksCountBySpecialId(dto);
        //凤凰结果进行返回
        PageResponseResult pageResponseResult =new PageResponseResult(dto.getPage(),dto.getSize(),count);
        pageResponseResult.setResult(worksList);
        return pageResponseResult;
    }

    /**
     * 修改作品是否在前台显示状态
     * @param special
     * @param showStatus
     */
    private void updateShowSpec(Special special, Integer showStatus){
        special.setShowFront(String.valueOf(showStatus));
        specialMapper.updateById(special);
    }
}
